package view;




import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import net.miginfocom.swing.MigLayout;

/**
 * This class manages the search view
 * 
 * The search view contains buttons for search, clear operations. It also holds text fields to 
 * capture the user's input for to, from, subject and body fields.
 * The search results are displayed in a table for clear presentation with a scroll pane for navigation.
 * 
 * @author MUSAB
 *
 * @version $Revision: 1.0 $
 */
public class SearchView extends JPanel {
	
	/**
	 * Matches label
	 */
	private JLabel matches;
	
	/**
	 * Buttons
	 */
	private JButton searchBtn, clearBtn;
	
	/**
	 * Text fields
	 */
	private JTextField to, from, subject, body;
	
	/**
	 * Emails table
	 */
	private DefaultTableModel emails;
	
	/**
	 * JTable
	 */
	private JTable emailsTable;
	
	/**
	 * Scroll pane
	 */
	private JScrollPane scrollPane;

	/**
	 * Constructor
	 */
	public SearchView(){
		
		super(new MigLayout("fillx, width 600"));
		initComponents();
		setupComponents();
		
	}

	/**
	
	 * @return the searchBtn */
	public JButton getSearchBtn() {
		return searchBtn;
	}

	/**
	
	 * @return the clearBtn */
	public JButton getClearBtn() {
		return clearBtn;
	}

	/**
	
	 * @return the to */
	public JTextField getTo() {
		return to;
	}

	/**
	
	 * @return the from */
	public JTextField getFrom() {
		return from;
	}

	/**
	
	 * @return the subject */
	public JTextField getSubject() {
		return subject;
	}

	/**
	
	 * @return the body */
	public JTextField getBody() {
		return body;
	}

	/**
	
	 * @return the emails */
	public DefaultTableModel getEmails() {
		return emails;
	}

	/**
	
	 * @return the emailsTable */
	public JTable getEmailsTable() {
		return emailsTable;
	}

	/**
	 * Lays out the components into the view
	 */
	private void setupComponents() {
		
		this.add(new JLabel("Enter the criteria and press 'Search'"), "wrap, span 2");
		
		this.add(new JLabel("To: "), "width 60");
		this.add(to, "growx, wrap");
		
		this.add(new JLabel("From: "));
		this.add(from, "growx, wrap");
		
		this.add(new JLabel("Subject: "));
		this.add(subject, "growx, wrap");
		
		this.add(new JLabel("Body: "));
		this.add(body, "growx, wrap");
		
		JPanel buttonsPanel = new JPanel(new MigLayout("fillx, insets 0"));
		buttonsPanel.add(matches, "align left");
		buttonsPanel.add(clearBtn, "push, align right");
		buttonsPanel.add(searchBtn);
		this.add(buttonsPanel, "align right, skip 1, wrap, growx");
		
		this.add(scrollPane, "span 2, height 300, width 100%");
		
	}

	/**
	 * Initializes the components
	 */
	private void initComponents() {
		
		// Labels
		matches = new JLabel("0 matches");
		
		// Buttons
		searchBtn = new JButton("Search", IconsManager.getImageIcon("small/magnifier.png"));
		clearBtn = new JButton("Clear", IconsManager.getImageIcon("small/ui-text-field-clear.png"));
		
		// Text fields
		to = new JTextField();
		from = new JTextField();
		body = new JTextField();
		subject = new JTextField();
		
		// Emails list
		String[] columnNames = {
				"_id",
				"No",
                "To",
                "From",
                "Date/time",
                "Subject",
                "Folder"
		};
		emails = new DefaultTableModel();
		emails.setColumnIdentifiers(columnNames);
		
		
		// Table
		emailsTable = new JTable(emails){
			@Override
			public boolean isCellEditable(int row, int column){
			    return false;  
			}
		};
		emailsTable.getTableHeader().setReorderingAllowed(false);
		emailsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		emailsTable.removeColumn( emailsTable.getColumn("_id") );
		
		// Scroll pane
		scrollPane = new JScrollPane(emailsTable);
	}
	
	/**
	 * Sets the number of matches found
	
	 * @param total Integer
	 */
	public void setMatches(Integer total){
		matches.setText(total.toString() + " matches");
	}

	/**
	 * Resets everything in the view
	 */
	public void clearAll() {
		setMatches(0);
		to.setText("");
		from.setText("");
		subject.setText("");
		body.setText("");
		clearEmails();
	}

	/**
	 * Clears all emails in the emails
	 */
	public void clearEmails() {
		if (emails.getRowCount() > 0) {
		    for (int i = emails.getRowCount() - 1; i > -1; i--) {
		        emails.removeRow(i);
		    }
		}
	}
	
	/**
	 * Checks if all the fields are empty or not
	 * 
	
	 * @return true or false */
	public boolean allFieldsEmpty(){
		if (	to.getText().isEmpty() && 
				from.getText().isEmpty() &&
				subject.getText().isEmpty() &&
				body.getText().isEmpty()){
			return true;
		}
		return false;
	}
	
}
